package com.mingyizhudao.app.view.search.adpter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.dixiang.framework.utils.Utils;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.common.BaseViewHolder;
import com.mingyizhudao.app.entity.DiseaseEntity;
import com.mingyizhudao.app.entity.HospitalEntity;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.doctor.DoctorDetailsActivity;
import com.mingyizhudao.app.view.doctor.bean.DoctorsEntity;
import com.mingyizhudao.app.view.home.bean.DisNavsEntity;
import com.mingyizhudao.app.view.recommend.HospitalToDeptActivity;
import com.mingyizhudao.app.view.search.SearchDeptActivity;
import com.mingyizhudao.app.view.search.SearchDoctorActivity;
import com.mingyizhudao.app.view.search.bean.AdpterEntity;
import com.mingyizhudao.app.view.search.bean.SearchEntity;

import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class SearchMoreAdapter extends BaseAdapter{

	private Context context;
	private List<AdpterEntity> doctors = new ArrayList<AdpterEntity>();
	private List<AdpterEntity> diseases = new ArrayList<AdpterEntity>();
	private List<AdpterEntity> hospitals = new ArrayList<AdpterEntity>();
	private List<AdpterEntity> list = new ArrayList<AdpterEntity>();
	private ListView listview;
	
	private ArrayList<DisNavsEntity> disNavs;
	private String actionUrl  ,total0,total1,total2  ,flg ,type;
	
	public SearchMoreAdapter(Context context , ListView listview, ArrayList<DisNavsEntity> disNavs,String actionUrl ,String type){
		this.context = context;
		this.listview = listview;
		
		this.disNavs = disNavs;
		this.actionUrl = actionUrl;
		
		this.type = type;
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View currentView, ViewGroup arg2) {
		final AdpterEntity entity = list.get(arg0);
		SearchHolder holder = null;
		if(entity.isTitle()){
			currentView = LayoutInflater.from(context).inflate(R.layout.seach_title, null, false);
			TextView textView = (TextView) currentView.findViewById(R.id.tile);
			
			if(entity.isDoctor()){
				textView.setText("搜索医生");
				
			}
			
			else if (entity.isHospital()) {
				textView.setText("搜索医院");
			}
			
			else{
				textView.setText("搜索疾病");
			}
			
		} else if(entity.isBottom()){
			currentView = LayoutInflater.from(context).inflate(R.layout.seach_more, null, false);
			TextView textView = (TextView) currentView.findViewById(R.id.tile);
			TextView tv_total = (TextView) currentView.findViewById(R.id.tv_total);
			View shadow = currentView.findViewById(R.id.shadow);
			if(entity.isDoctor()){
				
				tv_total.setText("共 "+total0+" 条");
				
				textView.setText("更多医生");
				shadow.setVisibility(View.VISIBLE);
			}
			
			else if (entity.isHospital()) {
				textView.setText("更多医院");
				tv_total.setText("共 "+total2+" 条");
				shadow.setVisibility(View.GONE);
			}
			else{
				textView.setText("更多疾病");
				tv_total.setText("共 "+total1+" 条");
				shadow.setVisibility(View.VISIBLE);
			}
			
			textView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					
					
					
					
					
					list.clear();
					if(entity.isDoctor()){
						addDoctors(true);
						addDiseases(false);
						addHospitals(false);
					}
					else if (entity.isHospital()) {
						addDoctors(false);
						addDiseases(false);
						addHospitals(true);
					}
					else{
						addDoctors(false);
						addDiseases(true);
						addHospitals(false);
					}
					notifyDataSetChanged();
					listview.setSelection(0);
				}
			});
			
		}else{
			if(currentView == null || currentView.getTag() == null){
				currentView = LayoutInflater.from(context).inflate(R.layout.search_list, null, false);
				holder = new SearchHolder(currentView);
				currentView.setTag(holder);
			}else{
				holder = (SearchHolder) currentView.getTag();
			}
			holder.populateView(arg0, entity);
		}
		
		return currentView;
	}
	
	public void setData(String flg , SearchEntity searchEntity){
		this.flg = flg;
		list.clear();
		doctors.clear();
		diseases.clear();
		hospitals.clear();
		List<DoctorsEntity> list0 = null;
		List<DiseaseEntity> list1 = null;
		List<HospitalEntity> list2 = null;
		if ("0".equals(type)) {
			list0 = searchEntity.getDoctors();
		}else if ("1".equals(type)) {
			list1 = searchEntity.getDiseases();
			
		}else if ("2".equals(type)) {
			
			list2 = searchEntity.getHospitals();
		}
		
		if(list0 != null && list0.size() > 0){
			total0 = ""+list0.size();
			AdpterEntity title = new AdpterEntity();
			title.setTitle(true);
			title.setDoctor(true);
			doctors.add(title);
			for (DoctorsEntity item : list0) {
				AdpterEntity entity = new AdpterEntity();
				entity.setId(item.getId());
				entity.setName(item.getName());
				entity.setHpName(item.getHpName());
				entity.setDptName(item.getHpDeptName());
				entity.setmTitle(item.getmTitle());
				entity.setaTitle(item.getaTitle());
				entity.setIsContracted(item.getIsContracted());;
				entity.setDoctor(true);
				doctors.add(entity);
			}
		}
		
		if(list1 != null && list1.size() > 0){
			total1 = ""+list1.size();
			AdpterEntity title = new AdpterEntity();
			title.setTitle(true);
			title.setDoctor(false);
			diseases.add(title);
			for (DiseaseEntity item : list1) {
				AdpterEntity entity = new AdpterEntity();
				entity.setId(item.getId());
				entity.setDiseaseName(item.getName());
				entity.setDoctor(false);
				diseases.add(entity);
			}
		}
		
		if(list2 != null && list2.size() > 0){
			total2 = ""+list2.size();
			AdpterEntity title = new AdpterEntity();
			title.setTitle(true);
			title.setHospital(true);
			
			hospitals.add(title);
			for (HospitalEntity item : list2) {
				AdpterEntity entity = new AdpterEntity();
				entity.setId(""+item.getId());
				entity.setHpName(item.getName());
				entity.setHospital(true);
				hospitals.add(entity);
			}
		}
		
		
		addDoctors(true);
		addDiseases(true);
		addHospitals(true);
	}
	
	private void addDiseases(boolean isAll){
		
		if(diseases.size() > 0){
			if(isAll){
				list.addAll(diseases);
			}else{
				if(diseases.size() > 4){
					list.addAll(diseases.subList(0, 4));
					AdpterEntity entity = new AdpterEntity();
					entity.setBottom(true);
					entity.setDoctor(false);
					list.add(entity);
				}else if(diseases.size() <= 4){
					list.addAll(diseases);
				}
			}
		}
	}
	
	private void addDoctors(boolean isAll){
		
		if(doctors.size() > 0){
			if(isAll){
				list.addAll(doctors);
			}else{
				if(doctors.size() > 4 ){
					list.addAll(doctors.subList(0, 4));
					AdpterEntity entity = new AdpterEntity();
					entity.setBottom(true);
					entity.setDoctor(true);
					list.add(entity);
				}else if(doctors.size() <= 4){
					list.addAll(doctors);
				}
			}
			
		}
	}
	
	
     private void addHospitals(boolean isAll){
		
		if(hospitals.size() > 0){
			if(isAll){
				list.addAll(hospitals);
			}else{
				if(hospitals.size() > 4 ){
					list.addAll(hospitals.subList(0, 4));
					AdpterEntity entity = new AdpterEntity();
					entity.setBottom(true);
					entity.setHospital(true);
					list.add(entity);
				}else if(hospitals.size() <= 4){
					list.addAll(hospitals);
				}
			}
			
		}
	}
	
     private SpannableStringBuilder getStringBuilder(String text) {
    	 if (text==null) {
    		 text="";
		}
         SpannableStringBuilder builder = new SpannableStringBuilder(text);
         ForegroundColorSpan yellowSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.actionbar_bg_color));
        
         if (flg!=null&&flg.length()>0&&text.contains(flg)) {
        	 builder.setSpan(yellowSpan, text.indexOf(flg), text.indexOf(flg)+flg.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
         
		return builder;
	}
	class SearchHolder extends BaseViewHolder<AdpterEntity>{

		private View view_doctor;
		private View view_diseases;
		private View view_hospitals;
		private TextView doctor_name,hp_name,dsc,diseases_name,hospitals_name;
		private Button btn_dpt,btn_doctor;
		public SearchHolder(View view) {
			super(view);
			view_doctor = view.findViewById(R.id.view_doctor);
			view_diseases = view.findViewById(R.id.view_diseases);
			view_hospitals = view.findViewById(R.id.view_hospitals);
			
			doctor_name = (TextView) view.findViewById(R.id.doctor_name);
			hp_name = (TextView) view.findViewById(R.id.hp_name);
			dsc = (TextView) view.findViewById(R.id.dsc);
			diseases_name = (TextView) view.findViewById(R.id.diseases_name);
			btn_dpt = (Button) view.findViewById(R.id.btn_dpt);
			btn_doctor = (Button) view.findViewById(R.id.btn_doctor);
			
			hospitals_name = (TextView) view.findViewById(R.id.hospitals_name);
		}	
		
		@Override
		public void populateView(int position, final AdpterEntity item) {
			if(item.isDoctor()){
				view_doctor.setVisibility(View.VISIBLE);
				view_diseases.setVisibility(View.GONE);
				view_hospitals.setVisibility(View.GONE);
				doctor_name.setText(getStringBuilder(item.getName()));
				hp_name.setText(getStringBuilder(item.getHpName()));
				String mTitle = Utils.isStringEmpty(item.getmTitle()) ? " " : ","+item.getmTitle();
				String aTitle = Utils.isStringEmpty(item.getaTitle()) ? " " : ","+item.getaTitle();
				dsc.setText(getStringBuilder(item.getDptName() + mTitle + aTitle));
				view_doctor.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						//选中医生
						
						DoctorsEntity doctorListEntity = new DoctorsEntity();
						doctorListEntity.setId(item.getId());
						doctorListEntity.setName(item.getName());
						doctorListEntity.setHpName(item.getHpName());
						doctorListEntity.setHpDeptName(item.getHpDeptName());
						doctorListEntity.setmTitle(item.getmTitle());
						doctorListEntity.setaTitle(item.getaTitle());
						doctorListEntity.setIsContracted(item.getIsContracted());
						
						
						Bundle bundle = new Bundle();
						bundle.putSerializable("doctorListEntity", doctorListEntity);
						IntentHelper.getInstance(context).gotoActivity(DoctorDetailsActivity.class, bundle);
						
					}
				});
			}
			
			
			else if (item.isHospital()) {
				view_doctor.setVisibility(View.GONE);
				view_diseases.setVisibility(View.GONE);
				view_hospitals.setVisibility(View.VISIBLE);
				hospitals_name.setText(getStringBuilder(item.getHpName()));
				
				view_hospitals.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Bundle bundle = new Bundle();
						HospitalEntity hospitalEntity = new HospitalEntity();
						hospitalEntity.setId(Integer.parseInt(item.getId()));
						hospitalEntity.setName(item.getHpName());
						
						bundle.putSerializable("hospitalEntity", hospitalEntity);
						bundle.putSerializable("hospitalViewUrl", Constants.baseURL+"hospital/");
						IntentHelper.getInstance(context).gotoActivity(HospitalToDeptActivity.class, bundle);
					}
				});
				
				
			}
			
			else{
				view_doctor.setVisibility(View.GONE);
				view_diseases.setVisibility(View.VISIBLE);
				view_hospitals.setVisibility(View.GONE);
				
				diseases_name.setText(getStringBuilder(item.getDiseaseName()));
				btn_dpt.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						//找科室操作
						Bundle bundle = new Bundle();
						bundle.putSerializable(Constants.ACTION_URL_DISNAVS,(Serializable) disNavs);
						bundle.putString("diseaseId", item.getId());
						bundle.putString("diseaseName", item.getDiseaseName());
						IntentHelper.getInstance(context).gotoActivity(SearchDeptActivity.class, bundle);
						
					}
				});
				
				btn_doctor.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						//找医生操作
						Bundle bundle = new Bundle();
						bundle.putSerializable(Constants.ACTION_URL_DISNAVS,(Serializable) disNavs);
						bundle.putString(Constants.PARM_ACTION_URL, actionUrl);
						bundle.putString("diseaseId", item.getId());
						bundle.putString("diseaseName", item.getDiseaseName());
						IntentHelper.getInstance(context).gotoActivity(SearchDoctorActivity.class, bundle);
						
					}
				});
			}
		}
	}
}
